package com.pro.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.annotation.LogMethod;
import com.pro.beans.User;
import com.pro.dao.UserDao;
import com.pro.mybatis.MyBatisExpression;
import com.pro.mybatis.SearchCondition;
import com.pro.param.ResponseParam;

@Controller
public class UserController extends BaseController{
	
	@Resource
	public UserDao userDao;
	
	@LogMethod("注销用户")
	@RequestMapping("/logout.do")
	public String logout(HttpSession httpSession){
		httpSession.removeAttribute("user");
		return "login";
	}
	@RequestMapping("loadUserBySearch.json")
	@ResponseBody
	public ResponseParam<User> loadUserBySearch(@ModelAttribute("requestParam")com.pro.param.RequestParam requestParam){
		SearchCondition sc = new SearchCondition();
		sc.and(MyBatisExpression.eq("type", requestParam.getType()));
		sc.setPage(requestParam.getPage());
		List<User> userList = userDao.loadAlarmListBySearchCd(sc);
		this.setDataList(userList);
		this.setSuccess(true);
		return this.getResponseParam();
	}
	
	@LogMethod("登陆")
	@RequestMapping("/login.do")
	public String Login(@ModelAttribute User user,Model model,HttpSession httpSession){
		if(user.getName()==null || user.getPassword()==null){
			return "login";
		}
		user = userDao.getObjectByObject(user);
		if(user == null){
			 model.addAttribute("msg","用户名或者密码错误");
			 return "login";
		}
		if(user.getDel_flag()!=null && user.getDel_flag()!=0){
			model.addAttribute("msg","用户被删除。");
			return "login";
		}
		httpSession.setAttribute("user",user);
		return "index";
	}
	@LogMethod("个人详细信息")
	@RequestMapping("/userinfo.do")
	public String userinfo(@RequestParam(value="id",required=false)Integer id,Model model,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("user");
		if(id!=null){
			user = userDao.getObject(id);
		}
		model.addAttribute("user", user);
		return "userinfo";
	}
	/*@LogMethod("加载菜单")
	@RequestMapping(value="/left.action",method=RequestMethod.GET)
	public String left(@RequestParam("type") String type,Model model,HttpSession httpSession){
		User user = (User)httpSession.getAttribute("user");
		Menu[] urls = null;
		if(user.getType()!=null && user.getType()==4){//管理员
			type="admin_"+type;
		}
//		else{
//			urls = LeftSetting.map.get(type);
//		}
		urls = LeftSetting.map.get(type);
		model.addAttribute("urls",urls);
		return "frame/left";
	}*/
	@LogMethod("账户加载")
	@RequestMapping("/account.do")
	public String account(@ModelAttribute User user,Model model){
		return "account";
	}
	/*@LogMethod("进入主页面")
	@RequestMapping("/main.do")
	public String main(){
//		System.out.println("name:"+user.getName()+",age:"+user.getName());
		return "main";
	}*/
	@LogMethod("获取所有用户列表")
	@RequestMapping("/getAllUser.do")
	public String getAllUser(@ModelAttribute User user,Model model){
//		userDao.addUser(user);
		List<User> list = userDao.getAllObjects();
		model.addAttribute("userList", list);
		return "index.do";
	}
	@LogMethod("更新用户")
	@RequestMapping("/updateUser.do")
	public String updateUser(@ModelAttribute User user,Model model,HttpSession httpSession){
//		userDao.addUser(user);
//		List<User> list = userDao.getList();
		int i = userDao.updateObject(user);
		if(i>0){
			user = userDao.getObject(user.getId());
			updateSession( httpSession);
		}
		model.addAttribute("jspName", "userinfo");
		model.addAttribute("menuId", "4");
		return "index";
	}
	
	@RequestMapping("loadUserById.json")
	@ResponseBody
	public ResponseParam<User> loadUserById(@RequestParam("id")Integer id ){
		User user = userDao.getObject(id);
		this.setData(user);
		this.setSuccess(true);
		return this.getResponseParam();
	}
	@LogMethod("修改密码")
	@RequestMapping("modifyPassword.do")
	public String modifyPassword(@RequestParam("newPassword")String newPassword,HttpSession httpSession,Model model){
		User user = (User)httpSession.getAttribute("user");
//		Integer accountId = user.getId();
		user.setPassword(newPassword);
		int i = userDao.updateObject(user);
		if(i>0){
			updateSession( httpSession);
			model.addAttribute("msg", "密码修改成功!");
		}
		model.addAttribute("jspName", "userinfo");
		model.addAttribute("menuId", "4");
		return "index";
	}

	@LogMethod("注册")
	@RequestMapping("addUser.do")
	public String addUser(HttpSession httpSession,Model model,@ModelAttribute("user")User user,@RequestParam(value="newPassword",required=false)String newPassword){
//		user.setPassword(newPassword);
		User hasUser = userDao.getObjectByObject(user);
		if(hasUser!=null){
			model.addAttribute("msg", "用户已经存在");
		}
		if(newPassword==null){
			newPassword = "123456";
		}
		user.setPassword(newPassword);
		userDao.addObject(user);
		model.addAttribute("jspName", "userList");
		model.addAttribute("menuId", "4");
		return "index";
	}
	@LogMethod("进入修改密码页面")
	@RequestMapping("enterModifyPwd.do")
	public String enterModifyPwd(HttpSession httpSession,Model model,@RequestParam(value="type",required=false)String type){
		if(type!=null){
			model.addAttribute("type", type);
			model.addAttribute("action", "addUser.do");
			return "modifyPwd";
		}
		User user = (User)httpSession.getAttribute("user");
//		user.setPassword(newPassword);
		String password = user.getPassword();
		model.addAttribute("action", "modifyPassword.do");
		model.addAttribute("password",password);
		return "modifyPwd";
	}
	@LogMethod("进入修改密码页面")
	@RequestMapping("getUserNameById.do")
	@ResponseBody
	public User getUserNameById(@RequestParam("accountName")String accountName){
		String[] s = accountName.split("EAccount_");
//		String msg = null;
		if(s.length<=1){
//			msg="账号名称格式不正确";
		}else{
			Integer id = Integer.valueOf(s[1]);
			User user = userDao.getObject(id);
			if(user==null){
//				msg="账号不存在";
			}else{
				return user;
			}
		}
		return new User();
	}
	public void updateSession(HttpSession httpSession){
		User user = (User)httpSession.getAttribute("user");
		Integer accountId = user.getId();
		user = userDao.getObject(accountId);
		httpSession.setAttribute("user", user);
	}
}
